package com.limao.CacheManager;

import java.util.HashMap;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.Admin.DBProvider.AdminSysParmProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

/**
 * 基础数据缓存，放置基本不修改的数据，后台基础资料
 * @author Administrator
 *
 */
public class BaseDataCacheManager  extends BasicModule{
	
	private AdminSysParmProvider provider;
	private static BaseDataCacheManager instance;

    public static BaseDataCacheManager getInstance() {
        return instance;
    }
 
    private Cache<String, String> baseDataCache;

    public BaseDataCacheManager() {
    	super("baseDataCacheManager");
    	baseDataCache = CacheFactory.createCache("cd_baseDataCache");
    	
    	initProvider();
    }
    
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    

    public void ClearObjectCache(String key)
    {
    	baseDataCache.remove(key);
    }  
    
    public void reLoad_Param_ALl()
    {
       	//取出全部参数
    	HashMap<String, String> params=provider.load_Params();
    	for (String key : params.keySet()) 
    	{
    		baseDataCache.put(key, params.get(key));
     	}
    }


	public String Load_Param(String  paramkey)
    {
    	if(CacheClass.isEmpty(paramkey)) 
    		return null;
    	String paramsValue=baseDataCache.get(paramkey);
    	
    	if (paramsValue == null) {
            synchronized (paramkey.intern()) {
            	
            	paramsValue=baseDataCache.get(paramkey);
                
                if (paramsValue == null) {
                	//取出全部参数
                	HashMap<String, String> params=provider.load_Params();
                	for (String key : params.keySet()) 
                	{
                		baseDataCache.put(key, params.get(key));
                		if(CacheClass.myEqualsIgnoreCase(key, paramkey))
                		{
                			paramsValue= params.get(key);
                		}
                 	}
                }
            }
    	}
	 
    	return paramsValue;
    }
    
	
	
	public void Set_Param(String  paramkey,String paramsValue)
    {
    	if(CacheClass.isEmpty(paramkey)) 
    		return ;
    	baseDataCache.put(paramkey,paramsValue.trim());
    }
   
    
//******************************************************
    private void initProvider() {
        if (provider == null ) {
            try {
                provider =AdminSysParmProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
    }
  
}


